{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* blog post: [Region of interest pooling explained - deepsense.io](https://deepsense.io/region-of-interest-pooling-explained/)\n",
    "* repository: [deepsense-io/roi-pooling](https://github.com/deepsense-io/roi-pooling)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "\n",
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "\n",
    "from roi_pooling.roi_pooling_ops import roi_pooling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 4x4 feature map with only 1 channel\n",
    "input_value = [[\n",
    "    [[1], [2], [4], [4]],\n",
    "    [[3], [4], [1], [2]],\n",
    "    [[6], [2], [1], [7]],\n",
    "    [[1], [3], [2], [8]]\n",
    "]]\n",
    "input_value = np.asarray(input_value, dtype='float32')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# regions of interest as lists of:\n",
    "# feature map index, upper left, bottom right coordinates\n",
    "rois_value = [\n",
    "    [0, 0, 0, 1, 3],\n",
    "    [0, 2, 2, 3, 3],\n",
    "    [0, 1, 0, 3, 2]\n",
    "]\n",
    "rois_value = np.asarray(rois_value, dtype='int32')\n",
    "\n",
    "# in this case we have 3 RoI pooling operations:\n",
    "# * channel 0, rectangular region (0, 0) to (1, 3)\n",
    "#              xx..\n",
    "#              xx..\n",
    "#              xx..\n",
    "#              xx..\n",
    "#\n",
    "# * channel 0, rectangular region (2, 2) to (3, 3)\n",
    "#              ....\n",
    "#              ....\n",
    "#              ..xx\n",
    "#              ..xx\n",
    "# * channel 0, rectangular region (1, 0) to (3, 2)\n",
    "#              ....\n",
    "#              xxx.\n",
    "#              xxx.\n",
    "#              xxx."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[[ 3.  4.]\n",
      "   [ 6.  3.]]]\n",
      "\n",
      "\n",
      " [[[ 1.  7.]\n",
      "   [ 2.  8.]]]\n",
      "\n",
      "\n",
      " [[[ 4.  4.]\n",
      "   [ 4.  7.]]]]\n"
     ]
    }
   ],
   "source": [
    "input_featuremap = tf.placeholder(tf.float32)\n",
    "rois = tf.placeholder(tf.int32)\n",
    "input_const = tf.constant(input_value, tf.float32)\n",
    "rois_const = tf.constant(rois_value, tf.int32)\n",
    "y = roi_pooling(input_const, rois_const, pool_height=2, pool_width=2)\n",
    "\n",
    "with tf.Session('') as sess:\n",
    "    y_output = sess.run(y, feed_dict={input_featuremap: input_value, rois: rois_value})\n",
    "    print(y_output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
